package com.summer.framework.common.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.summer.framework.common.entity.response.BooleanResponse;
import com.summer.framework.common.entity.response.ListResponse;
import com.summer.framework.common.entity.response.ObjectResponse;
import com.summer.framework.common.service.BaseService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * @auther: 杨杰
 * @date: 2018/11/12 18:07
 * @description:
 */
@Slf4j
@RestController
public abstract class BaseController<S extends BaseService<T>, T> {

    @Autowired
    protected S baseService;

    @PostMapping()
    protected BooleanResponse save(@RequestBody T t) {
        return new BooleanResponse("", baseService.save(t));
    }

    @GetMapping("/{id}")
    protected ObjectResponse<T> getById(@PathVariable String id) {
        return new ObjectResponse(baseService.getById(id));
    }

    @GetMapping("")
    protected ListResponse<T> getAll(T t) {
        return new ListResponse(baseService.list(new QueryWrapper<T>()));
    }

    @GetMapping("/page/{current}/{size}")
    protected ObjectResponse page(@PathVariable Integer current, @PathVariable Integer size, T t) {
        return new ObjectResponse(baseService.page(current, size, t));
    }

    @GetMapping("/isExist/{cloumName}/{value}")
    protected BooleanResponse isExist(@PathVariable String cloumName, @PathVariable String value) {
        boolean result = baseService.isExist(cloumName, value);
        return new BooleanResponse<Boolean>(result == true ? "数据库存在" : "数据库不存在", result);
    }

    @GetMapping("/isNotExist/{cloumName}/{value}")
    protected BooleanResponse isNotExist(@PathVariable String cloumName, @PathVariable String value) {
        boolean result = baseService.isNotExist(cloumName, value);
        return new BooleanResponse<Boolean>(result == true ? "数据库不存在" : "数据库存在", result);
    }

    @PutMapping("")
    protected BooleanResponse update(@RequestBody T t) {
        return new BooleanResponse("", baseService.updateById(t));
    }

    @DeleteMapping("")
    protected BooleanResponse delete(@RequestParam("ids") String[] ids) {
        return new BooleanResponse("刪除成功", baseService.deleteByIds(ids));
    }

}

